<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      name="file"
      :show-file-list="false"
      :headers="headers"
      style="display: inline-block; vertical-align: top"
    >
      <el-icon v-if="!srcValue"><Plus /></el-icon>
      <!-- <el-image v-if="!srcValue" :src="srcValue">
        <div slot="error" class="image-slot">
        </div>
      </el-image> -->
      <div v-else class="image">
        <el-image
          :src="srcValue"
          :style="`width:150px;height:150px;`"
          fit="fill"
        />
        <div class="mask">
          <div class="actions">
            <span @click.stop="dialogVisible = true">
              <el-icon style="color: #fff"><ZoomIn /></el-icon>
            </span>
            <span style="margin-left: 10px" @click.stop="removeImage">
              <el-icon style="color: #fff"><Delete /></el-icon>
            </span>
          </div>
        </div>
      </div>
    </el-upload>
    <el-dialog
      :close-on-press-escape="false"
      v-model="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <img
        :src="srcValue"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { getToken } from "@/utils/auth";
import {
  ElMessage,
  ElMessageBox,
  ElNotification,
  ElLoading,
} from "element-plus";
const { proxy } = getCurrentInstance();
const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  },
});
const { modelValue } = toRefs(props);
const emit = defineEmits();
const srcValue = computed({
  get() {
    return modelValue.value;
  },
  set(value) {
    emit("update:modelValue", value);
  },
});
// console.log("srcValue", srcValue.value);

const dialogVisible = ref(false);
let uploadImgUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 使用时换成自己项目的路径
const headers = ref({
  Authorization: "Bearer " + getToken(),
});

function removeImage() {
  emit("handleInput", "");
}

function handleUploadSuccess(res) {
  emit("handleInput", res.url);
  proxy.$modal.closeLoading();
}

function handleBeforeUpload() {
  proxy.$modal.loading("上传中...");
}

function handleUploadError() {
  ElMessage({
    type: "error",
    message: "上传失败",
  });
  proxy.$modal.closeLoading();
}
</script>

<style scoped lang="scss">
.image {
  position: relative;
  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
    .actions {
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  &:hover .mask {
    opacity: 1;
    .actions {
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
